package com.hdyg.sixg.activity.mine;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.AreaAdapter;
import com.hdyg.sixg.app.MvpApplication;
import com.hdyg.sixg.entry.MineBean;
import com.hdyg.sixg.mvp.Contrant.CPhone;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PPhone;
import com.hdyg.sixg.util.CountDownTimerUtil;
import com.hdyg.sixg.util.PopWindowUtil;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 手机号码
 * @time 2019/4/8 14:59
 */
public class PhoneActivity extends BaseActivity implements CPhone.IVPhone {

    public static final int GET_CODE_TYPE = 0x00001;    //手机号码
    public static final int BIND_PHONE_TYPE = 0x00002;  //绑定新手机

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.sv_username)
    SuperTextView svUsername;
    @BindView(R.id.sv_area)
    SuperTextView svArea;
    @BindView(R.id.sv_old_phone)
    SuperTextView svOldPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_code)
    TextView tvCode;
    @BindView(R.id.bt_next)
    Button btNext;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    @BindView(R.id.et_new_phone)
    EditText etNewPhone;
    @BindView(R.id.ll_new_phone)
    LinearLayout llNewPhone;

    private CPhone.IPPhone mPresenter;
    private int phoneType;
    private String code, phone, new_quhao, username;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_phone;
    }

    @Override
    protected void initView() {
        onClickListener();
        tvTopTitle.setText(R.string.phone_title);
        phoneType = getIntent().getIntExtra(SpMsg.INTENT_CODE, GET_CODE_TYPE);
        username = getIntent().getStringExtra(SpMsg.USERNAME);
        svUsername.setRightString(username);
        if (phoneType == GET_CODE_TYPE) {
            phone = getIntent().getStringExtra(SpMsg.USER_PHONE);
            svOldPhone.setRightString(phone);
            llNewPhone.setVisibility(View.GONE);
            svArea.setVisibility(View.GONE);
            svOldPhone.setVisibility(View.VISIBLE);
            btNext.setText(R.string.phone_next);
        } else {
            llNewPhone.setVisibility(View.VISIBLE);
            svArea.setVisibility(View.VISIBLE);
            svOldPhone.setVisibility(View.GONE);
            btNext.setText(R.string.phone_finish);
        }
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());
        //获取验证码
        tvCode.setOnClickListener(v -> {
            if (phoneType == GET_CODE_TYPE) {
                //获取旧手机验证码
                if (TextUtils.isEmpty(phone)) {
                    toastNotifyShort(R.string.regist_phone_hint);
                    return;
                }
            } else {
                phone = etNewPhone.getText().toString().trim();
                if (TextUtils.isEmpty(new_quhao)) {
                    toastNotifyShort(R.string.regist_area_select);
                    return;
                }
                if (TextUtils.isEmpty(phone)) {
                    toastNotifyShort(R.string.phone_new_phone_hint);
                    return;
                }
            }
            mPresenter.pGetCode(BaseUrl.SEND_CODE_URL, GetParamUtil.getSendCodeParam(new_quhao, phone, BaseUrl.CODE_TYPE_BIND_PHONE));
        });
        //区域选择
        svArea.setOnSuperTextViewClickListener(superTextView -> {
           showAreaPop(superTextView);
        });
        //下一步
        btNext.setOnClickListener(v -> {
            if (phoneType == GET_CODE_TYPE) {
                //验证旧手机
                code = etCode.getText().toString().trim();
                if (TextUtils.isEmpty(code)) {
                    toastNotifyShort(R.string.regist_code_hint);
                    return;
                }
                if (StringUtil.isFastDoubleClick()){
                    return;
                }
                mPresenter.pGetCheckPhone(BaseUrl.CHECK_PHONE_URL, GetParamUtil.getCheckPhoneParam(code));
            } else {
                //绑定新手机
                code = etCode.getText().toString().trim();
                phone = etNewPhone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    toastNotifyShort(R.string.phone_new_phone_hint);
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    toastNotifyShort(R.string.regist_code_hint);
                    return;
                }
                if (TextUtils.isEmpty(new_quhao)) {
                    toastNotifyShort(R.string.regist_area_select);
                    return;
                }
                if (StringUtil.isFastDoubleClick()){
                    return;
                }
                mPresenter.pGetApplyPhone(BaseUrl.BIND_PHONE_URL, GetParamUtil.getBindPhoneParam(code, new_quhao, phone));
            }

        });
    }

    @Override
    protected void initData() {
        if (phoneType == GET_CODE_TYPE) {
            mPresenter.pGetInit(BaseUrl.PHONE_URL, GetParamUtil.getPhoneParam());
        }
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PPhone(this);
    }

    private void showAreaPop(View v) {
        if (MvpApplication.countryBeanList == null) {
            toastNotifyShort(R.string.toast_area);
            return;
        }
        View view = View.inflate(mContext, R.layout.pop_area, null);
        RecyclerView rvArea = view.findViewById(R.id.rv_area);
        rvArea.setLayoutManager(new LinearLayoutManager(mContext));
        AreaAdapter areaAdapter = new AreaAdapter(R.layout.item_area, MvpApplication.countryBeanList);
        rvArea.setAdapter(areaAdapter);
        areaAdapter.setOnItemClickListener((adapter, view1, position) -> {
            svArea.setRightString(MvpApplication.countryBeanList.get(position).getCountry())
            .setRightTextColor(getResources().getColor(R.color.white));
            new_quhao = MvpApplication.countryBeanList.get(position).getCode();
            mPopupWindow.dismiss();
        });
        mPopupWindow = PopWindowUtil.getInstance().makePopupWindow(mContext, view, ViewGroup.LayoutParams.MATCH_PARENT)
                .showAsDropDown(mContext, v, 0);

    }

    /**
     * 初始化回调
     *
     * @param dataBean
     */
    @Override
    public void vGetInitSuccess(MineBean dataBean) {
        username = dataBean.getUsername();
        phone = dataBean.getPhone();
        new_quhao = dataBean.getQuhao();
        svUsername.setRightString(username);
        svOldPhone.setRightString(phone);
    }

    /**
     * 获取验证码回调
     *
     * @param dataBean
     */
    @Override
    public void vGetCodeSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        CountDownTimerUtil.getInstance().startTimer(tvCode, 60);
    }

    /**
     * 验证旧手机回调
     *
     * @param dataBean
     */
    @Override
    public void vGetCheckPhoneSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        mBundle = new Bundle();
        mBundle.putInt(SpMsg.INTENT_CODE, BIND_PHONE_TYPE);
        mBundle.putString(SpMsg.USERNAME, username);
        startActivity(PhoneActivity.class, mBundle);
        finish();
    }

    /**
     * 修改手机号码回调
     *
     * @param dataBean
     */
    @Override
    public void vGetApplyPhoneSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        finish();
    }
}
